package com.googlecode.continuity.plugin.gwtext.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventListener;
import com.google.gwt.user.client.ui.*;

/**
 * PopupMenu
 * <p/>
 * TODO: create new gwt comp module and move there
 * TODO: add submenus
 *
 * @author Peter Schmitt <aragos@aragos.de>
 */
public class PopupMenu extends PopupPanel {
  private FlexTableWithListeners list;

  public PopupMenu() {
    super(true);
    list = new FlexTableWithListeners();
    addStyleName("popupMenu");
    setWidget(list);
  }

  public void addLine(String label, final ClickListener callback, String style, String explanation,
                      final boolean active) {
    final int rowCount = list.getRowCount();
    final HTML text = new HTML("<span>" + label + "</span>");
    list.setWidget(rowCount, 0, text);
    final HTMLTable.RowFormatter rowFormatter = list.getRowFormatter();
    final Element row = rowFormatter.getElement(rowCount);

    // set mouse handling on row
    list.setRowListener(row, new EventListener() {
      public void onBrowserEvent(Event event) {
        int type = DOM.eventGetType(event);
        switch (type) {
          case Event.ONCLICK:
            if (active) {
              callback.onClick(text);
              rowFormatter.removeStyleName(rowCount, "hover");
              hide();
            }
            break;
          case Event.ONMOUSEOVER:
            // assert ONMOUSEENTER
            Element from = DOM.eventGetFromElement(event);
            if (!DOM.isOrHasChild(rowFormatter.getElement(rowCount), from)) {
              rowFormatter.addStyleName(rowCount, "hover");
            }
            break;
          case Event.ONMOUSEOUT:
            // assert ONMOUSEOUT
            from = DOM.eventGetFromElement(event);
            if (!DOM.isOrHasChild(rowFormatter.getElement(rowCount), from)) {
              rowFormatter.removeStyleName(rowCount, "hover");
            }
            break;
          case Event.ONMOUSEDOWN:
            DOM.eventPreventDefault(event);
            break;
        }
      }
    });

    // set row styles
    rowFormatter.addStyleName(rowCount, "line");
    rowFormatter.setVerticalAlign(rowCount, HasVerticalAlignment.ALIGN_MIDDLE);
    if (style != null) {
      rowFormatter.addStyleName(rowCount, style);
    }
    if (!active) {
      rowFormatter.addStyleName(rowCount, "inactive");
    }

    // text style
    text.addStyleName("label");
    text.setWordWrap(false);

    // add explanation
    if (explanation == null || explanation.equals("")) {
      list.getFlexCellFormatter().setColSpan(rowCount, 0, 2);
    } else {
      Label expl = new Label(explanation);
      expl.addStyleName("explanation");
      list.setWidget(rowCount, 1, expl);
//      list.getFlexCellFormatter().setHorizontalAlignment(rowCount, 1, HasHorizontalAlignment.ALIGN_RIGHT);
    }
  }

  public void addLine(String label, ClickListener callback) {
    addLine(label, callback, null, null, true);
  }

  public void addLine(String label, ClickListener callback, String style) {
    addLine(label, callback, style, null, true);
  }

  public void addLine(String label, ClickListener callback, String style, String explanation) {
    addLine(label, callback, style, explanation, true);
  }

  public void removeLine(String label) {
    for (int i = 0; i < list.getRowCount(); i++) {
      if (((Label) list.getWidget(i, 0)).getText().equals(label)) {
        list.removeRow(i);
        list.removeRowListener(list.getRowFormatter().getElement(i));
        return;
      }
    }
  }

  public void addSeperator() {
    if (list.getRowCount() > 0) {
      list.getRowFormatter().addStyleName(list.getRowCount() - 1, "separatorBelow");
    }
  }

  public void showAt(int x, int y) {
    setPopupPosition(x, y);
    show();
  }
}
